package com.example.opengldemo.sky;

import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;
import com.example.opengldemo.util.ShapeFactory;

public class SkyBall {
    ShaderHandle shaderHandle ;
    ShaderBuffer shaderBuffer ;

    String vertexPath = "sky/vertex.sh" ;
    String fragPath = "sky/fragment.sh" ;

    final float UNIT_SIZE = 140f ;

    public SkyBall(View view){
        String vertexRes = ShaderUtil.loadFromAssetsFile(vertexPath , view.getResources());
        String fragRes = ShaderUtil.loadFromAssetsFile(fragPath , view.getResources());
        int program = ShaderUtil.createProgram(vertexRes , fragRes);
        shaderHandle = new ShaderHandle(program);
        shaderBuffer = new ShaderBuffer();

        shaderBuffer.setVertexBuffer(getVertexArray());
        shaderBuffer.setTexcoorBuffer(getTexcoorsArray());


    }
    public float[] getVertexArray(){
        return ShapeFactory.getEarthVertexArray(UNIT_SIZE, 10);
    }

    public float[] getTexcoorsArray(){
        return ShapeFactory.getEarthgenerateTexCoor(10) ;
    }

    public void drawSelf(int texture) {
        shaderHandle.program()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindMVPMaxtrixHandle()
                .bindDefaultTextureHandle(texture)
                .run(GLES30.GL_TRIANGLES ,  shaderBuffer.vCount);
    }
}
